import axios from 'axios'
import Qs from 'qs'
import Vue from 'vue'

// 创建axios实例
const service = axios.create({
  // api的base_url
  baseURL: process.env.NODE_ENV === 'development' ? '/wechat' : 'https://www.felixmantuoluo.com/wechat/',
  timeout: 80000 // 请求超时时间
})

// request拦截器
service
  .interceptors
  .request
  .use(request => {
    request.headers['Token'] = localStorage.getItem('token')
    request.transformRequest = [data => {
      return typeof data === 'object' && request.headers['Content-Type'] !== 'multipart/form-data' ?
        Qs.stringify(data) :
        data
    }]
    Vue.$vux.loading.show({
      text: '加载中'
    })
    return request
  }, error => {
    console.log(error)
  })

// respone拦截器
service
  .interceptors
  .response
  .use(response => {
    Vue.$vux.loading.hide()
    return response
  }, error => {
    if (window.plus) {
      window
        .plus
        .nativeUI
        .closeWaiting()
    }
    return Promise.reject(error)
  })

export default service
